﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightMonster : NetUnionFightBase
    {
        public Dictionary<long, InstUFCreep> monsters = new Dictionary<long, InstUFCreep>();


        public NetUnionFightMonster(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {
        }

        public override void Enter()
        {
            base.Enter();
            monsters.Clear();
        }

        public override void Leave()
        {
            base.Leave();
            monsters.Clear();
        }

        public  RoomCharactor CreateCharactor(long uuid,Vector3 pos)
        {
            var inst = monsters[uuid];
            charactorManager.CreateUnionMonster(monsters[uuid], pos);
            postions[uuid] = pos;
            monsters[uuid] = inst;
            return GetCharactor(uuid);
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);
            if(monsters.ContainsKey(uuid))
            {
                monsters.Remove(uuid);
            }
        }

        /// <summary>
        /// 刷新野怪
        /// </summary>
        /// <param name="msg"></param>
        public void MonsterRefresh(SCUFCreep msg)
        {
            if (msg != null)
            {
                for (var i = 0; i < msg.creep.Count; ++i)
                {
                    var monster = msg.creep[i];
                    var uuid = msg.creep[i].guid;
                    var pos = msg.local[i];
                    monsters[uuid] = monster;
                    var _pos = new Vector3(pos.x, pos.y, pos.z);


                    var charactor = GetCharactor(uuid, false);
                    if (charactor == null)
                    {
                        charactor = CreateCharactor(uuid, _pos);
                    }

                    states[uuid] = msg.creep[i].state;
                    if (states[uuid] == (int)UnionMonsterState.Death)
                    {
                        UpdateState(uuid, msg.creep[i].state, null);
                    }
                    else
                    {
                        UpdateState(uuid, msg.creep[i].state);
                    }

                    Debug.Log("[MONSTER state]=(" + (UnionMonsterState)msg.creep[i].state + ")");

                    UpdatePosition(uuid, new Vector3(pos.x, pos.y, pos.z));

                    Debug.Log("uuid=" + uuid + " " + "[MONSTER position] monster postion:" + postions[uuid]);

                    UpdateHp(msg.creep[i].fightInfo);
                    if (msg.angle != null && msg.angle.Count > i + 1)
                    {
                        UpdateAngle(uuid, msg.angle[i]);
                    }
                }
            }
        }
        /// <summary>
        /// 野怪出生
        /// </summary>
        /// <param name="msg"></param>
        public void MonsterBorn(SCUFCreepSpawn msg)
        {
            for (int i = 0; i < msg.creep.Count; ++i)
            {
                var uuid = msg.creep[i].guid;
                var monster = msg.creep[i];
                var pos = msg.pos[i];
                postions[uuid] = new Vector3(pos.x, pos.y, pos.z);
                monsters[uuid] = monster;
                CreateCharactor(uuid, postions[uuid]);
                var state = (int)UnionMonsterState.Stand;
                UpdateState(uuid, state);
                UpdateHp(msg.creep[i].fightInfo);
                UpdatePosition(uuid, new Vector3(pos.x, pos.y, pos.z));
                if (msg.angle != null && msg.angle.Count > i + 1)
                {
                    UpdateAngle(uuid, msg.angle[i]);
                }
            }
        }
        /// <summary>
        /// 野怪战斗开始
        /// </summary>
        /// <param name="msg"></param>
        public void MonsterFightBegin(SCUFMonsterFightBegin msg)
        {
            var uuid = msg.guid;
            var state = (int)UnionMonsterState.Attack;
            UpdateState(uuid, state);
        }

        /// <summary>
        /// 野怪战斗结束
        /// </summary>
        /// <param name="msg"></param>
        public void MonsterFightEnd(SCUFMonsterFightEnd msg)
        {
            var uuid = msg.guid;
            monsters[uuid].fightInfo = msg.fightSide;
            var state = (int)UnionMonsterState.Stand;
            UpdateState(uuid, state);
            UpdateHp(msg.fightSide);
        }

        /// <summary>
        /// 野外怪死亡
        /// </summary>
        /// <param name="msg"></param>
        public void MonsterDead(SCUFMonsterDead msg)
        {
            var uuid = msg.guid;
            var state = (int)UnionMonsterState.Death;
            UpdateState(uuid, state);
        }

    }
}